//
//  ConciergeLoginViewController.m
//  StanfordEvents
//
//  Created by Gongmyung on 7/12/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ConciergeLoginViewController.h"


@implementation ConciergeLoginViewController

@synthesize userName;
@synthesize password;
@synthesize delegate;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (IBAction) login {
    //create a get request
    NSURL *url = [NSURL URLWithString:@"http://colby.stanford.edu/community_vibe/concierge/rpc.php"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    
    [request setPostValue:userName.text forKey:@"name"];
    [request setPostValue:password.text forKey:@"key"];
    [request setPostValue:@"authenticatePrincipal" forKey:@"do"];
    NSLog(@"About to start authenticate request with username: %@, key: %@", userName.text, password.text);
    [request startSynchronous];
    NSLog(@"Finished authenticate, return value is: %@", [request responseString]);
    NSString *response;
    NSError *error = [request error];
    if (!error) {
        response = [request responseString];
        NSLog(response);
    
        if ( [response isEqualToString:@"false"] ) {
            //pop up dialog that asks if the user is certain that they want to send to this contact
            UIAlertView *failedLogin = [[UIAlertView alloc]
                                        initWithTitle:@"Login Failed"
                                        message:@"Invalid username/password. Please try again."
                                        delegate:self
                                        cancelButtonTitle:@"Ok"
                                        otherButtonTitles: nil];
            [failedLogin show];
            [failedLogin autorelease];
        }
        else {
            NSLog(@"Login success, about to quit login screen");
            //store username and password in plist file
            NSUserDefaults *defaultSettings = [NSUserDefaults standardUserDefaults];
            [defaultSettings setObject:userName.text forKey:@"username"];
            [defaultSettings setObject:password.text forKey:@"password"];
            [defaultSettings setObject:response forKey:@"pid"];
            [defaultSettings synchronize];
            
            //send device token to concierge
            
            [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
             UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
             
            [self.view removeFromSuperview];
            [self.delegate finished];
        }
    }
    else {
        //pop up dialog that shows that concierge has failed
        UIAlertView *failedLogin = [[UIAlertView alloc]
                                    initWithTitle:@"Login Failed"
                                    message:@"Unable to contact concierge - make sure you have a valid connection"
                                    delegate:self
                                    cancelButtonTitle:@"Ok"
                                    otherButtonTitles: nil];
        [failedLogin show];
        [failedLogin autorelease];
    }
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
